<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="archsw0904.controller.authentication.*"%>
<%@ page import="java.util.Map"%>
<%@ page import="java.util.HashMap"%>
<%@ page import="java.net.URLEncoder"%>
<jsp:useBean id="usercache" scope="session"
	class="session.cache.UserCache" />
<%
	request.setCharacterEncoding(application
			.getInitParameter("PARAMETER_ENCODING"));
	String action = request.getParameter("action");
	Boolean badLogIndata = false;
	String refererOverride = request.getParameter("refererOverride");
	String addBook = request.getParameter("addBook");

	if (action != null) {
		AuthenticationManagerServiceLocator authServiceLocation = new AuthenticationManagerServiceLocator();
		authServiceLocation
				.setAuthenticationManagerPortEndpointAddress(application
						.getInitParameter("AUTHENTICATION_MANAGER_WEB_SERVICE_URI"));
		AuthenticationManager authServiceport = authServiceLocation
				.getAuthenticationManagerPort();

		if (action.compareToIgnoreCase("login") == 0) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			if (username == null)
				username = "";
			if (password == null)
				password = "";
			int token = authServiceport.logIn(username, password);

			if (token != -1) {
				Cookie cookie = new Cookie("token", token + "");
				cookie.setPath("/");
				response.addCookie(cookie);

				if (refererOverride != null && addBook != null) {
					String redicect = "/BookShopWeb/page/cart.jsp?refererOverride="
							+ URLEncoder.encode(refererOverride)
							+ "&addBook=" + URLEncoder.encode(addBook);
					System.out.println("Redicerc creato: " + redicect);
					response.sendRedirect(response
							.encodeRedirectURL(redicect));
				} else if (refererOverride != null) {
					response.sendRedirect(response
							.encodeRedirectURL(refererOverride));
				} else {
					//Non devo aggiungere nessun libro, lo mando alla home
					response.sendRedirect(response
							.encodeRedirectURL("/BookShopWeb/"));
				}
			} else {
				badLogIndata = true;
			}
		}

		if (action.compareToIgnoreCase("logout") == 0) {
			Cookie cookies[] = request.getCookies();
			Cookie tokenCookie = null;

			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().compareToIgnoreCase("token") == 0) {
					tokenCookie = cookies[i];

				}
			}

			if (tokenCookie != null) {

				int token = Integer.parseInt(tokenCookie.getValue());
				authServiceport.logOut(token);

				Cookie killMyCookie = new Cookie("token", null);
				killMyCookie.setMaxAge(0);
				killMyCookie.setPath("/");
				response.addCookie(killMyCookie);
				usercache.resetChace();
			} else {

			}
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
<jsp:include page="/partial/head_menu.jsp" />
<div>
<form action="<%="/BookShopWeb/page/login.jsp?action=login"%>"
	method="post">
<%
	if (badLogIndata) {
%>
<p><font color="red"><b>Wrong username or password</b></font></p>
<%
	}
%>
<table>
	<tbody>
		<tr>
			<td><b>Username</b></td>
			<td><input type="text" name="username" size="12" /></td>
		</tr>
		<tr>
			<td><b>Password</b></td>
			<td><input type="password" name="password" size="12" /></td>
		</tr>
	</tbody>
</table>
<%
	Map<String, String> params = request.getParameterMap();
	for (String key : params.keySet()) {
		if (key.compareToIgnoreCase("username") != 0
				&& key.compareToIgnoreCase("password") != 0
				&& key.compareToIgnoreCase("action") != 0) {
%> <input type="hidden" name="<%=key%>"
	value="<%=request.getParameter(key)%>" /> <%
 	}
 	}
 %> <input type="submit" value="LogIn" /></form>
</div>
</body>
</html>